package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao {
    private static Student[] stus = new Student[5];    //需要static修饰，此处不添加static，无法共享，每次调用Dao方法时都是出现创建数组

    public  void deleteId(String id) {
        int index = getIndex(id);
        stus[index] = null;

    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu!=null&&stu.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;
    }


    public boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu1 = stus[i];
            if (stu1 == null) {
                index = i;
                break;     //取到一个值就结束,不然循环一直进行
            }
        }
        //for循环结束了之后,如果index还是-1,则说明数组装满了,
        if (index == -1) {
            //装满了,
            return false;
        } else {
            //还有位置,将数组存进去
            stus[index] = stu;//stu为封装好传进来的信息
            return true;
        }
    }

    public Student[] findALLStudent() {
        return stus;

    }

    public void updateStuent(String id,String name,String age,String birthday) {
        int index = 0;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu.getId().equals(id)){
                index = i ;
                break;
            }
        }
        stus[index].setId(id);
        stus[index].setName(name);
        stus[index].setAge(age);
        stus[index].setBirthday(birthday);
    }
}
